class BrowerWebSocketClient {
    static _EventCallBack={};

    constructor(wsUrl) {
        this.webSocket = new WebSocket(wsUrl)
        this.webSocket.binaryType = 'arraybuffer';
        this.webSocket.onmessage = function(event){
            let req=event;
            let that=this;
            if(typeof(event.data)=="string")  {
                req.params= event.data;
            }else {
                req.params= new Uint8Array(event.data);
            }

            let res={}
            res.send=function (v){
                that.send(v)
            }
            BrowerWebSocketClient._EventCallBack['onmessage']? BrowerWebSocketClient._EventCallBack['onmessage'](req,res):console.log(' onmessage');
        }
        this.webSocket.onopen = function(event) {
            let req={}
            req.params=event;
            BrowerWebSocketClient._EventCallBack['onopen']? BrowerWebSocketClient._EventCallBack['onopen'](req,null):console.log(' onopen');
        }

        this.webSocket.onclose = function(event) {
            BrowerWebSocketClient._EventCallBack['onclose']? BrowerWebSocketClient._EventCallBack['onclose']():console.log(' onclose');
        }
    }

    set(url, callback) {
        BrowerWebSocketClient._EventCallBack[url]=callback;
    }

    send(buffer) {
        if(Array.isArray(buffer)){
            buffer=new Uint8Array(buffer)
        }
        if(this.webSocket.readyState === WebSocket.OPEN){
            this.webSocket.send(buffer)
        }
    }

}

